package com.tweet_o_matic.models;

import java.util.ArrayList;
import static com.tweet_o_matic.helpers.LogHelpers.info;
import java.util.UUID;

import android.util.Log;

public class TweetPortfolio
{
  public  ArrayList<Tweet>  tweets;
  private PortfolioSerialiser   serialiser;

  public TweetPortfolio(PortfolioSerialiser serialiser)
  {
    this.serialiser = serialiser;
    try
    {
      tweets = serialiser.loadTweets();
    }
    catch (Exception e)
    {
      info(this, "Error loading tweets: " + e.getMessage());
      tweets = new ArrayList<Tweet>();
      this.generateTestData();
    }
  }  
  
  public boolean saveTweets()
  {
    try
    {
      serialiser.saveResidences(tweets);
      info(this, "Tweets saved to file");
      return true;
    }
    catch (Exception e)
    {
      info(this, "Error saving tweets: " + e.getMessage());
      return false;
    }
  }  
  
  public void addTweet(Tweet tweet)
  {
	  tweets.add(tweet);
  }

  public Tweet getTweet(UUID id)
  {
    Log.i(this.getClass().getSimpleName(), "UUID parameter id: "+ id);

    for (Tweet twt : tweets)
    {
      if(id.equals(twt.id))
      {
        return twt; 
      }
    }
    return null;
  }

  private void generateTestData()
  {
    for(int i = 0; i < 5; i += 1)
    {
      Tweet t = new Tweet();
      t.message = "Test message number " + (0 + i);
      tweets.add(t);
    }
  }
}